import { Controller } from '@nestjs/common'
import { MenuService } from './menu.service'
import { Body, Post, Req, UseGuards } from '@nestjs/common/decorators'
import { ApiBearerAuth, ApiConsumes, ApiOperation, ApiTags } from '@nestjs/swagger'
import { CreateMenuDto } from './dto/create-menu.dto'
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'
import { ListMenuDto } from './dto/list-menu.dto'
import { UpdateMenuDto } from './dto/update-menu.dto'
import { delMenuDto } from './dto/del-menu.dto'
import { Request } from 'express'
import { CreateOrderDto } from '../order/dto/order.dto'
import { RateLimit } from '../common/rateLimit/rate-limit.decorator'

@ApiTags('menu')
@Controller('menu')
export class MenuController {
  constructor(private readonly menuService: MenuService) { }
  // 新增菜品
  @ApiOperation({ summary: '新增菜品' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(10, 5 * 60 * 1000)
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('JWT')
  @Post('create')
  async create(@Body() data: CreateMenuDto, @Req() req: Request) {
    await this.menuService.create({ ...data, user_id: req.user?.id as number })
    return {
      isAdd: true
    }
  }

  // 菜品列表
  @ApiOperation({ summary: '菜品list' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(30, 60 * 1000)
  @Post('list')
  async list(@Body() query: ListMenuDto, @Req() req: Request) {
    const body = await this.menuService.list({ ...query, user_id: req.user?.id })
    return body
  }

  // 更新菜品
  @ApiOperation({ summary: '更新菜品' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(10, 5 * 60 * 1000)
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('JWT')
  @Post('update')
  async update(@Body() data: UpdateMenuDto, @Req() req: Request) {
    await this.menuService.update({ ...data, user_id: req.user?.id as number })
    return {
      isAdd: true
    }
  }

  // 删除菜品
  @ApiOperation({ summary: '删除菜品' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(15, 5 * 60 * 1000)
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('JWT')
  @Post('delete/:id')
  async delete(@Body() data: delMenuDto) {
    await this.menuService.delete(data)
    return {
      isAdd: true
    }
  }

  // 下单
  @ApiOperation({ summary: '下单' })
  @RateLimit(5, 60 * 1000)
  @UseGuards(JwtAuthGuard)
  @ApiBearerAuth('JWT')
  @Post('placeOrder')
  async handleOrder(@Body() data: CreateOrderDto, @Req() req: Request) {
    const userId = req.user?.id as number
    await this.menuService.handleOrder(data, userId)
    return {
      isAdd: true
    }
  }

  // 热门菜品
  @ApiOperation({ summary: '热门菜品' })
  @ApiConsumes('application/x-www-form-urlencoded')
  @RateLimit(30, 60 * 1000)
  @Post('recommend')
  async recommendMenu() {
    return this.menuService.recommendMenu()
  }
}